package com.theme.passport.controller.app;

import com.theme.common.base.ConfigConstant;
import com.theme.common.mvc.view.R;
import com.theme.passport.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("app/system")
@Api("系统相关配置链接文档:[关于我们,教程与帮助,隐私政策]")
public class SystemHelpController {

    @Autowired
    private SysConfigService sysConfigService;

    private final static String ABOUTUS_KEY = ConfigConstant.SYS_ABOUT_US_CONFIG_KEY;

    private final static String TUTORIALS_KEY = ConfigConstant.SYS_TUTORIALS_CONFIG_KEY;

    private final static String PRIVACYPOLICY_KEY = ConfigConstant.SYS_PRIVACY_POLICY_CONFIG_KEY;

    private final static String SERVICE_TERMS_KEY = ConfigConstant.SYS_SERVICE_TERMS_CONFIG_KEY;

    private final static String HELP_KEY = ConfigConstant.SYS_HELP_CONFIG_KEY;

    private final static String THEME_HELP_KEY = ConfigConstant.SYS_THEME_HELP_CONFIG_KEY;

    private final static String WIDGE_HELP_KEY = ConfigConstant.SYS_WIDGE_HELP_CONFIG_KEY;
    private final static String ZUIDIVERSION = ConfigConstant.ZUIDIVERSION;
    private final static String DANGQIANVERSION = ConfigConstant.DANGQIANVERSION;

    @GetMapping("/aboutUs")
    @ApiOperation("关于我们")
    public R aboutUs(){
        String value = sysConfigService.getValue(ABOUTUS_KEY);
        return R.ok().put("aboutUs", value);
    }

    @GetMapping("/serviceTerms")
    @ApiOperation("服务条款")
    public R serviceTerms(){
        String value = sysConfigService.getValue(SERVICE_TERMS_KEY);
        return R.ok().put("serviceTerms", value);
    }

    @GetMapping("/privacyPolicy")
    @ApiOperation("隐私政策")
    public R privacyPolicy(){
        String value = sysConfigService.getValue(PRIVACYPOLICY_KEY);
        return R.ok().put("privacyPolicy", value);
    }

    @GetMapping("/course")
    @ApiOperation("系统教程")
    public R course(){
        String value = sysConfigService.getValue(TUTORIALS_KEY);
        return R.ok().put("help", value);
    }

    @GetMapping("/help")
    @ApiOperation("系统帮助")
    public R help(){
        String help = sysConfigService.getValue(HELP_KEY);
        String themeHelp = sysConfigService.getValue(THEME_HELP_KEY);
        String widgeHelp = sysConfigService.getValue(WIDGE_HELP_KEY);
        Map<String,Object> map = new HashMap<>();
        map.put("index",help);
        map.put("themeHelp",themeHelp);
        map.put("widgeHelp",widgeHelp);
        return R.ok().put("help", map);
    }

    @GetMapping("/version")
    @ApiOperation("系统帮助")
    public R version(String version){
        String zuidiversion = sysConfigService.getValue(ZUIDIVERSION);
        String dangqianversion = sysConfigService.getValue(DANGQIANVERSION);

        int zuidiresult = compareVersion(version, zuidiversion);

        int ruoresult = compareVersion(version, dangqianversion);
        Map<String,Object> map = new HashMap<>();
        map.put("zuidiversion",zuidiversion);
        map.put("dangqianversion",dangqianversion);
        map.put("qiangzhigengxin",zuidiresult < 0 ? true:false);
        map.put("ruogengxin",ruoresult < 0 ? true:false);
        return R.ok().put("version", map);
    }

    public  static  int compareVersion(String version1, String version2) {
        String[] v1 = version1.split("\\.");
        String[] v2 = version2.split("\\.");
        int len = Math.max(v1.length, v2.length);
        for (int i = 0; i < len; i++) {
            int num1 = i < v1.length ? Integer.parseInt(v1[i]) : 0;
            int num2 = i < v2.length ? Integer.parseInt(v2[i]) : 0;
            if (num1 < num2) {
                return -1;
            } else if (num1 > num2) {
                return 1;
            }
        }
        return 0;
    }
}
